package com.cloud.core.exception;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;

public class MyException extends RuntimeException {
    private Integer code;

    private String message;

    private String errorCode; // 新增字段来存储原始的异常代码


    public MyException(String message) {
        this.errorCode = message;
       Locale locale = LocaleContextHolder.getLocale();
        MessageSource messageSource = SpringUtil.getBean(MessageSource.class);
   //     LocaleResolver locale = SpringUtil.getBean(LocaleResolver.class);
        String msg = messageSource.getMessage(message, null, message, locale);
        this.message = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public MyException(String message, Integer code) {
        this.errorCode = message;
        Locale locale = LocaleContextHolder.getLocale();
        MessageSource messageSource = SpringUtil.getBean(MessageSource.class);
        String msg = messageSource.getMessage(message, null, message, locale);

        this.message = msg;
        this.code = code;
    }



    public String getErrorCode() { // 新增方法来获取原始的异常代码
        return errorCode;
    }

}
